{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 集合概述与创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{2, 3, 4, 5, 6, 7, 8}\n",
      "{0, 1, 2, 3, 4, 5} <class 'set'>\n",
      "{1, 2, 4, 5, 6} <class 'set'>\n",
      "{1, 2, 3, 4, 5, 65} <class 'set'>\n",
      "{'h', 'o', 't', 'n', 'y', 'P'} <class 'set'>\n",
      "{1, 2, 4, 6} <class 'set'>\n",
      "<class 'dict'>\n",
      "set() <class 'set'>\n"
     ]
    }
   ],
   "source": [
    "# 集合是没有value的字典，是可变类型（列表、字典）\n",
    "# 集合中的元素是无序的，不允许重复元素\n",
    "# 集合的创建方式\n",
    "# 第一种：使用{}\n",
    "s = {2, 3, 4, 5, 5, 6, 7, 8}  # 集合中的元素不允许重复\n",
    "print(s)\n",
    "# 第二种：使用内置函数set()\n",
    "s1 = set(range(6))\n",
    "print(s1, type(s1))\n",
    "s2 = set([1, 2, 4, 5, 5, 6, 6])  # 去掉了重复元素\n",
    "print(s2, type(s2))\n",
    "s3 = set((1, 2, 3, 4, 4, 5, 65))  # 元组类型的元素通过set转为集合元素\n",
    "print(s3, type(s3))\n",
    "s4 = set('Python')  # 字符串类型的元素通过set转为集合元素\n",
    "print(s4, type(s4))  # 无序，且变为一个一个的字母\n",
    "s5 = set({1, 2, 4, 4, 6})\n",
    "print(s5, type(s5))\n",
    "# 空集合的定义\n",
    "s6 = {}  # dict字典类型\n",
    "print(type(s6))\n",
    "s7 = set()\n",
    "print(s7, type(s7))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 集合相关操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "False\n",
      "True\n",
      "{10, 80, 20, 405, 60}\n",
      "{200, 10, 300, 80, 400, 20, 405, 60}\n",
      "{100, 200, 10, 300, 45, 46, 47, 80, 400, 78, 20, 405, 90, 60}\n",
      "{200, 10, 300, 45, 46, 47, 80, 400, 78, 20, 405, 90, 60}\n",
      "{10, 300, 45, 46, 47, 80, 400, 78, 20, 405, 90, 60}\n",
      "set()\n"
     ]
    }
   ],
   "source": [
    "s = {10, 20, 405, 60}\n",
    "# 判断操作\n",
    "print(10 in s)\n",
    "print(100 in s)\n",
    "print(10 not in s)\n",
    "print(100 not in s)\n",
    "# 新增操作\n",
    "s.add(80)  # 一次一个\n",
    "print(s)\n",
    "s.update({200, 300, 400})  # 一次多个元素，至少一个，集合\n",
    "print(s)\n",
    "s.update([45, 46, 47])  # 列表\n",
    "s.update((78, 90, 100))  # 元组\n",
    "print(s)\n",
    "# 删除操作\n",
    "s.remove(100)\n",
    "print(s)\n",
    "# s.remove(500)  # KeyError: 500   不存在抛出异常\n",
    "s.discard(500)  # 不存在不抛出异常\n",
    "s.pop()  # 一次删除一个任意元素，不可以指定参数\n",
    "print(s)\n",
    "s.clear()  # 清空元素\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. 集合关系"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "True\n",
      "False\n",
      "True\n",
      "False\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 两个集合是否相等（元素相同就相等）\n",
    "s = {10, 20, 30, 40}\n",
    "s1 = {30, 40, 20, 10}\n",
    "print(s == s1)\n",
    "print(s != s1)  # 集合中的元素是无序的\n",
    "\n",
    "# 一个集合是否是另一个集合的子集\n",
    "s2 = {10, 20, 30, 40, 50, 60}\n",
    "s3 = {10, 20, 30, 40}\n",
    "s4 = {10, 20, 90}\n",
    "print(s3.issubset(s2))\n",
    "print(s4.issubset(s2))\n",
    "\n",
    "# 一个集合是否是另一个集合的超集\n",
    "print(s2.issuperset(s3))\n",
    "print(s2.issuperset(s4))\n",
    "\n",
    "# 两个集合是否没有交集\n",
    "print(s3.isdisjoint(s4))  # 有交集为false\n",
    "s5 = {100, 200, 300}\n",
    "print(s3.isdisjoint(s5))  # 没有交集为true"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4. 集合数据操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{40, 20, 30}\n",
      "{40, 20, 30}\n",
      "{40, 10, 50, 20, 60, 30}\n",
      "{40, 10, 50, 20, 60, 30}\n",
      "{10}\n",
      "{10}\n",
      "{50, 10, 60}\n",
      "{50, 10, 60}\n"
     ]
    }
   ],
   "source": [
    "# 集合的数学操作\n",
    "# 1.交集\n",
    "s1 = {10, 20, 30, 40}\n",
    "s2 = {20, 30, 40, 50, 60}\n",
    "print(s1.intersection(s2))\n",
    "print(s1 & s2)  # intersection与&等价，交集操作，s1与s2未发生变化\n",
    "\n",
    "# 2.并集\n",
    "print(s1.union(s2))  # 去除重复元素\n",
    "print(s1 | s2)  # union与|等价，并集操作，s1与s2未发生变化\n",
    "\n",
    "# 3.差集\n",
    "print(s1.difference(s2))\n",
    "print(s1 - s2)  # difference与-等价，差集操作，s1与s2未发生变化\n",
    "\n",
    "# 4.对称差集\n",
    "print(s1.symmetric_difference(s2))\n",
    "print(s1 ^ s2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5. 集合生成式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 4, 9, 16, 25]\n",
      "{0, 1, 64, 4, 36, 9, 16, 49, 81, 25}\n"
     ]
    }
   ],
   "source": [
    "# 列表生成式\n",
    "lst = [i*i for i in range(6)]\n",
    "print(lst)\n",
    "\n",
    "# 集合生成式\n",
    "s = {i*i for i in range(10)}  # 无序\n",
    "print(s)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.3",
   "language": "python",
   "name": "python3.6.3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "480px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
